﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Base.cshtml";
}
@section head{
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.validate.min.js")"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.metadata.js")"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/messages_cn.js")"></script>
}
<div id="toptoolbar">
</div>
<div id="policygrid">
</div>
<form action="/Policy/PolicyAdd" method="post" id="frmPolicy" name="frmPolicy" style="background-color: white;
display: none; margin-left: 30px;">
 <div id="form1" class="liger-form" style="padding-top:10px">
 <table cellpadding="0" cellspacing="0" class="l-table-edit">
    <tr>
        <td align="right" class="l-table-edit-td">
            <span class="l-star">*</span>&nbsp;策略名称:
        </td>
        <td align="left" class="l-table-edit-td" colspan="2">
            <input name="txtPolicyName"  id="txtPolicyName" ltype="text" />
        </td>
    </tr>
    <tr>
        <td align="right" class="l-table-edit-td">
            <span class="l-star">*</span>&nbsp;结算周期:
        </td>
        <td align="left" class="l-table-edit-td">
            <input name="txtDuration"  ltype="text" id="txtDuration" ligerui="width:100" style="width:100;"/>
        </td>
        <td align="left">
            <input id="selDurationType" ltype="select" ligerui="width:65" style="width: 65;"/> 
        </td>
    </tr>
    <tr>
        <td align="right" class="l-table-edit-td">
            <span class="l-star">*</span>&nbsp;结算期数:
        </td>
        <td align="left" class="l-table-edit-td">
            <input name="txtCycleAmount"  ltype="text" id="txtCycleAmount" ligerui="width:100" style="width:100;"/>
        </td>
        <td align="left">
            <span>(0：与项目一致，非0：自定义)</span>
        </td>
    </tr>
    <tr>
        <td align="right" class="l-table-edit-td">
            <span class="l-star">*</span>&nbsp;计算方式:
        </td>
        <td align="left" class="l-table-edit-td" colspan="2">
         <input  id="selSettlementType" ltype="select" /> 
        </td>
    </tr>
    <tr>
        <td align="right" class="l-table-edit-td">
            <span class="l-star">*</span>&nbsp;结算方式:
        </td>
        <td align="left" class="l-table-edit-td" colspan="2"> 
            <input  id="selBalanceType" ltype="select" /> 
        </td>
    </tr>
    <tr>
        <td align="right" class="l-table-edit-td">
            <span class="l-star"></span>&nbsp;年化收益:
        </td>
        <td align="left" class="l-table-edit-td">
            <input name="txtRate" type="text" id="txtRate" ltype="text" ligerui="width:100" style="width:100;"/>
        </td>
        <td align="left">
            <span id="danwei" ligerui="width:60" style="width:60;"></span>
        </td>
    </tr>
    <tr>
        <td align="center" colspan="3">
            <span id="lblmsg" class="l-validate-error">&nbsp;</span>
        </td>
    </tr>
</table>
</div>
</form>
<script type="text/javascript">
    var policygrid;
    var selBalanceType;
    $(function () {
        $("#toptoolbar").ligerToolBar({ items: [{ text: '新增', click: grid_policy_add, icon: "add" },
            { text: '修改', click: grid_policy_edit, icon: "modify" },
            { text: '删除', click: grid_policy_delete, icon: "delete" },
            { text: '刷新', click: grid_policy_refresh, icon: "refresh" }
            ]
        });
        policygrid = $("#policygrid").ligerGrid({
            columns: [
                { display: '策略名称', name: 'PolicyName', width: '15%', isSort: false },
                { display: '结算间隔', name: 'DurationType', width: '8%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.DurationType) == 0) {
                            return item.Duration + "天";
                        }
                        else if (parseInt(item.DurationType) == 1) {
                            return item.Duration + "月";
                        }
                    }
                },
                { display: '结算期数', name: 'CycleAmount', width: '6%', isSort: false },
                { display: '计算方式', name: 'SettlementType', width: '8%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.SettlementType) == 0) return '按比例';
                        else if (parseInt(item.SettlementType) == 1) return '固定值';
                    }
                },
                { display: '年化收益', name: 'Rate', width: '6%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.SettlementType) == 0) {
                            return item.Rate + "%";
                        }
                        else if (parseInt(item.SettlementType) == 1) {
                            return item.Rate + "元";
                        }
                    }
                },
                { display: '结算方式', name: 'BalanceType', width: '10%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.BalanceType) == 0) return '每期等额本金利息';
                        else if (parseInt(item.BalanceType) == 1) return '每期结息到期还本';
                        else if (parseInt(item.BalanceType) == 2) return '到期结本息';
                        else if (parseInt(item.BalanceType) == 3) return '每期固定收益';
                        else if (parseInt(item.BalanceType) == 4) return '到期固定收益';
                    }
                },
                { display: '特殊策略', name: 'IsAttach', width: '8%', isSort: false,
                    render: function (item) {
                        if (item.IsAttach == 0) return '否';
                        else if (item.IsAttach == 1) return '是';
                    }
                },
                { display: '状态', name: 'Status', width: '6%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.Status) == 0) return '禁用';
                        else if (parseInt(item.Status) == 1) return '启用';
                    }
                },
                { display: '创建人', name: 'CreateUser', width: '8%', isSort: false },
                { display: '创建时间', name: 'CreateTime', width: '14%', isSort: false },
                {
                    display: '操作', isAllowHide: false,
                    render: function (row) {
                        var html = '<a href="#" onclick="policyDetails(\'' + row.ID + '\',\'' + row.PolicyName + '\',\'' + row.DurationType + '\',\'' + row.Duration + '\',\'' + row.BalanceType + '\',\'' + row.SettlementType + '\',\'' + row.Rate + '\',\'' + row.IsAttach + '\',\'' + row.CycleAmount + '\')">修改</a>&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.ID + '\', -1)">删除</a>';
                        if (row.Status == 1) {
                            html = html + '&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.ID + '\',0)">禁用</a>';
                        }
                        else if (row.Status == 0) {
                            html = html + '&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.ID + '\',1)">启用</a>';
                        }
                        return html;
                    }
                }
                ],
            width: '100%', height: '100%', rownumbers: true,
            dataAction: 'server',
            url: "/Policy/GetPolicyListByPage",
            pageSizeOptions: [20],
            pageSize: 20
        });

        //////////
        selBalanceType = $("#selBalanceType").ligerComboBox();
        $("#selDurationType").ligerComboBox({ width: 65, data: [{ id: 0, text: '天' }, { id: 1, text: '月'}] });
        $("#selSettlementType").ligerComboBox(
            {
                data: [{ id: 0, text: '按比例' }, { id: 1, text: '固定值'}],
                onSelected: function (value, text) {
                    initBalanceType(value);
                }
            });

        ///////////

        $.metadata.setType("attr", "validate");
        var v = $("#frmPolicy").validate({
            errorPlacement: function (lable, element) {
                if (element.hasClass("l-textarea")) {
                    element.ligerTip({ content: lable.html(), target: element[0] });
                }
                else if (element.hasClass("l-text-field")) {
                    element.parent().ligerTip({ content: lable.html(), target: element[0] });
                }
                else {
                    lable.appendTo(element.parents("td:first").next("td"));
                }
            },
            success: function (lable) {
                lable.ligerHideTip();
                lable.remove();
            },
            submitHandler: function () {
                var durationType = liger.get("selDurationType").selectedValue;
                var settlementType = liger.get("selSettlementType").selectedValue;
                var balanceType = liger.get("selBalanceType").selectedValue;

                if ($('#txtPolicyName').val() == "" || $('#txtPolicyName').val().length > 10) {
                    $('#txtPolicyName').focus();
                    $("#lblmsg").html('用户名不能为空且不超过10个字符');
                    return;
                }

                $("#lblmsg").html('');
                $.ajax({
                    url: "/Policy/PolicyAdd",
                    data: "policyName=" + $("#txtPolicyName").val() + "&durationType=" + durationType + "&balanceType=" + balanceType
                            + "&rate=" + $("#txtRate").val() + "&settlementType=" + settlementType + "&duration=" + $("#txtDuration").val()
                            + "&cycleAmount=" + $("#txtCycleAmount").val(),
                    type: "post",
                    success: function (result) {
                        if (result == "OK") {
                            $.ligerDialog.success("保存成功。");
                            saveDialog.hide();
                            policyreset();
                            grid_policy_refresh();
                        }
                        else {
                            $.ligerDialog.error("保存失败。");
                        }
                    },
                    error: function (msg) {
                        $.ligerDialog.error("保存失败。");
                    }
                });
            }
        });
    });

    function initBalanceType(settlementType) {
        if (settlementType == 0) {
            selBalanceType.setData([{ id: 0, text: '每期等额本金利息' }, { id: 1, text: '每期结息到期还本' }, { id: 2, text: '到期结本息'}]);
            $("#danwei").html("%");
        }
        else {
            selBalanceType.setData([{ id: 3, text: '每期固定收益' }, { id: 4, text: '到期固定收益'}]);
            $("#danwei").html("元");
        }
    }

    function search(page) {
        if (page == 1) {
            policygrid.setOptions(
             {
                 parms:
                [
                    { name: "page", value: 1 }
                ]
             });
        }
        else {
            policygrid.setOptions();
        }
        policygrid.loadData();
    }

    // 添加
    var saveDialog;
    function grid_policy_add() {
        $('#txtPolicyName').attr("disabled", false);
        liger.get("selDurationType").selectValue("1");
        liger.get("selSettlementType").selectValue("0");
        liger.get("selBalanceType").selectValue("0");
        saveDialog = $.ligerDialog.open({
            title: "添加特殊策略",
            id: 'policyDialog',
            name: 'policyDialog',
            slide: false,
            isHidden: false,
            modal: true,
            target: $("#frmPolicy"),
            width: 410, height: 330, isResize: false, allowClose: false,
            buttons: [
                    { text: '保存', onclick: save_policy },
                    {
                        text: '取消', onclick: function (item, dialog) {
                            dialog.hide();
                            policyreset();
                            grid_policy_refresh();
                        }
                    }
                ]
        });
    }

    function save_policy() {
        if ($('#txtPolicyName').val() == "" || $('#txtPolicyName').val().length > 20) {
            $('#txtPolicyName').focus();
            $("#lblmsg").html('用户名不能为空且不超过20个字符');
            return;
        }
        if ($('#txtDuration').val() == "") {
            $('#txtDuration').focus();
            $("#lblmsg").html('请输入结算周期');
            return;
        }
        if ($('#txtCycleAmount').val() == "") {
            $('#txtCycleAmount').focus();
            $("#lblmsg").html('请输入结算期数');
            return;
        }
        if ($('#txtRate').val() == "") {
            $('#txtRate').focus();
            $("#lblmsg").html('请输入年化收益');
            return;
        }
        var form = $('#frmPolicy');
        form.submit();
    }

    function policyreset() {
        $('#txtPolicyName').val("");
        $('#txtDuration').val("");
        $('#txtRate').val("");
        $('#txtCycleAmount').val("");
        $("#isEdit").val("0");
        $("#lblmsg").html("");
    }

    // 修改
    var editDialog;
    function grid_policy_edit() {
        if (policygrid.getSelecteds().length == 0) {
            $.ligerDialog.warn('选择要修改的记录！', '温馨提示');
            return;
        }
        var selPolicy = policygrid.getSelected();
        policyDetails(selPolicy.ID, selPolicy.PolicyName, selPolicy.DurationType, selPolicy.Duration, selPolicy.BalanceType, selPolicy.SettlementType, selPolicy.Rate, selPolicy.IsAttach, selPolicy.CycleAmount);
    }

    var selpolicyid;
    function policyDetails(policyId, policyName, durationType, duration, balanceType, settlementType, rate, isAttach, cycleAmount) {
        selpolicyid = policyId;
        liger.get("selDurationType").selectValue(durationType);
        liger.get("selSettlementType").selectValue(settlementType);
        liger.get("selBalanceType").selectValue(balanceType);
        $('#txtPolicyName').val(policyName);
        $('#txtPolicyName').attr("disabled", true);
        $('#txtDuration').val(duration);
        $('#txtRate').val(rate);
        $('#txtCycleAmount').val(cycleAmount);

        editDialog = $.ligerDialog.open({
            title: "修改策略",
            id: 'policyDialog',
            name: 'policyDialog',
            slide: false,
            isHidden: false,
            modal: true,
            target: $("#frmPolicy"),
            width: 410, height: 330, isResize: false, allowClose: false,
            buttons: [
                { text: '保存', onclick: policyEditSave },
                {
                    text: '取消', onclick: function (item, dialog) {
                        editDialog.hide();
                        policyreset();
                        grid_policy_refresh();
                    }
                }
            ]
        });
    }

    function policyEditSave() {
        var durationType = liger.get("selDurationType").selectedValue;
        var settlementType = liger.get("selSettlementType").selectedValue;
        var balanceType = liger.get("selBalanceType").selectedValue;

        if ($('#txtDuration').val() == ""){
            $('#txtDuration').focus();
            $("#lblmsg").html('请输入结算周期');
            return;
        }
        if ($('#txtCycleAmount').val() == "") {
            $('#txtCycleAmount').focus();
            $("#lblmsg").html('请输入结算期数');
            return;
        }
        if ($('#txtRate').val() == "") {
            $('#txtRate').focus();
            $("#lblmsg").html('请输入年化收益');
            return;
        }
        $("#lblmsg").html('');
        $.ajax({
            url: "/Policy/PolicyEdit",
            data: "policyId=" + selpolicyid + "&policyName=" + $("#txtPolicyName").val() + "&durationType=" + durationType + "&balanceType=" + balanceType
                    + "&rate=" + $("#txtRate").val() + "&settlementType=" + settlementType
                     + "&cycleAmount=" + $("#txtCycleAmount").val() + "&duration=" + $("#txtDuration").val(),
            type: "post",
            success: function (result) {
                if (result == "OK") {
                    $.ligerDialog.success("保存成功。");
                    editDialog.hide();
                    policyreset();
                    grid_policy_refresh();
                }
                else {
                    $.ligerDialog.error("保存失败。");
                }
            },
            error: function () {
                $.ligerDialog.error("保存失败。");
            }
        });
    }

    function grid_policy_delete() {
        var policyId = "";

        var selected = policygrid.getSelecteds();
        if (selected.length == 0) {
            $.ligerDialog.warn('至少选择一条记录进行删除！', '温馨提示');
            return;
        }

        for (var i = 0; i < selected.length; i++) {
            policyId += selected[i].ID + ",";
        }
        policyId = policyId.substring(0, policyId.length - 1);
        changestate(policyId, -1);

    }

    // 改变策略状态
    function changestate(policyid, destinatstatus) {
        var deststatus;
        if (destinatstatus == 1) {
            deststatus = "启用";
        }
        else if (destinatstatus == 0) {
            deststatus = "禁用";
        }
        else if (destinatstatus == -1) {
            deststatus = "删除";
        }

        $.ligerDialog.confirm('确定要' + deststatus + '选中的策略吗？', '温馨提示', function (result) {
            if (result) {
                $.ajax({
                    url: '/Policy/PolicyStatusChange',
                    data: "policyId=" + policyid + "&destinationStatus=" + destinatstatus,
                    type: "post",
                    success: function (result) {
                        if (result == "OK") {
                            $.ligerDialog.success(deststatus + "成功！");
                            grid_policy_refresh();
                        } else {
                            $.ligerDialog.error(deststatus + '失败！', '温馨提示');
                        }
                    }
                });
            }
        });
    }

    // 刷新
    function grid_policy_refresh() {
        policygrid.setOptions(
             {
                 parms:
                [
                    { name: "page", value: 1 }
                ]
             });
        policygrid.loadData();
    }
 
         
  
</script>
